package uk.ac.ebi.bioinvindex.model.impl.term;

import uk.ac.ebi.bioinvindex.model.impl.AnnotatableImpl;

import javax.persistence.*;
import java.util.Collection;

/**
 * User: Nataliya Sklyar (nsklyar@ebi.ac.uk)
 * Date: Jul 12, 2007
 */
@Entity
@Table(name = "property_value")

@DiscriminatorColumn(name = "obj_type", discriminatorType = DiscriminatorType.STRING, length = 255)

public abstract class PropertyValueImpl<PT extends PropertyImpl> extends FreeTextTermImpl {

	protected PT type;

	protected UnitValue unit;

	protected PropertyValueImpl() {
		super();
	}

	protected PropertyValueImpl(PT type) {
		this.type = type;
		type.addPropertyValue(this);
	}


	@ManyToOne(targetEntity = PropertyImpl.class, cascade = CascadeType.ALL)
	@JoinColumn(name = "PROPERTY_ID", nullable = false)
	public PT getType() {
		return this.type;
	}

	protected void setType(PT type) {
		this.type = type;
	}


	@ManyToOne(targetEntity = uk.ac.ebi.bioinvindex.model.impl.term.UnitValue.class,
			cascade = CascadeType.PERSIST)
	@JoinColumn(name = "unit")
	public UnitValue getUnit() {
		return this.unit;
	}

	public void setUnit(UnitValue unit) {
		this.unit = unit;
	}

	@OneToMany(
			targetEntity = OntologyTermImpl.class
	)
	@JoinTable(
			name = "PropertyValue2OT",
			joinColumns = {@JoinColumn(name = "PV_ID")},
			inverseJoinColumns = @JoinColumn(name = "OT_ID")
	)
	public Collection<OntologyTermImpl> getOntologyTerms() {
		return super.getOntologyTerms();
	}


	@Override
  public String toString () {
		String result = "PropertyValueImpl{ " +
	    "ID = " + getId() +
	    " Type = " + this.getType () +
	    " Value = " + this.getValue ();
		UnitValue unit = this.getUnit ();
		if ( unit != null ) result += unit.getValue ();
	  result += " }";
	  return result;
  }


}
